using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MouseGesturesAddIn {
	public partial class ConfigForm : Form {
		private Configuration configData;
		private SelectActionForm selectActionForm; 
		public ConfigForm() {
			InitializeComponent();
		}

		/// <summary>
		/// Loads configuration
		/// </summary>
		/// <param name="c">Configuration object</param>
		/// <param name="a">List of all available actions</param>
		public void LoadConfiguration(Configuration c, List<string> actions) {
      configData = c;

			foreach (ListViewItem item in listViewActions.Items) {
				item.SubItems[1].Text = configData[item.Text];
			}

			// Loads list of common actions
			List<string> commonActions = new List<string>();
			foreach (string action in actions) {
				if (Resources.common_actions.Contains(action))
					commonActions.Add(action);
			}

			selectActionForm = new SelectActionForm();
			selectActionForm.LoadConfiguration(actions, commonActions);
    }


		
		/// <summary>
		/// Saves configuration.
		/// </summary>
		public void SaveConfiguration() {
			foreach (ListViewItem item in listViewActions.Items) {
				configData[item.Text] = item.SubItems[1].Text;
			}

			configData.Save();
		}

		private void listViewActions_DoubleClick(object sender, EventArgs e) {
			EditMouseGesture();
		}

		private void btnSelectAction_Click(object sender, EventArgs e) {
			EditMouseGesture();
		}

		/// <summary>
		/// Shows dialog, where user can select action asociated with mouse gesture
		/// </summary>
		private void EditMouseGesture() {
			string action = listViewActions.SelectedItems[0].SubItems[1].Text;
			if (listViewActions.SelectedItems.Count > 0) {
				if (String.IsNullOrEmpty(action) == false) {
					if (selectActionForm.SelectAction(action) == false) {
						MessageBox.Show("Action '" + action + "' no longer available.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					}
				}

				if (selectActionForm.ShowDialog() == DialogResult.OK) {
					listViewActions.SelectedItems[0].SubItems[1].Text = selectActionForm.getSelectedAction();
				}
			}
		}
	}
}